#! /usr/bin/perl -w

use strict;


my $CI80 = 1;
my $CI90 = 2;
my $CI95 = 3;
my $CI98 = 4;
my $CI99 = 5;
my $CI99_8 = 6;
my $CI99_9 = 7;

### How tight should the CI be?
my $myCI = $CI95;

my $maxRuns = 20;
my $plusMinus = 0.05;


my @ttbl;

sub parse_t_distribution {
    ## Takes the t-distribution.tbl file, and parses it into a nice, fat array.

    open (T, "~/bin/t-distribution.tbl") || die "No T dist! $!";
    my $i =0;
    while(my $line = <T>) {
	my @tok = split /\s+/, $line;
	pop @tok;  ## Remove trailing n.

	while($i < $tok[0]) {
	    push @ttbl, \@tok;
	    $i++;
#	    print("$i " . join ("-", @tok) . "\n");
	}
    }

}

sub runit {
    my $cmd = shift;
    my $start_sec = `getTimeOfDay`;
    
    chomp($start_sec);
    
    system($cmd);
    
    my $end_sec = `getTimeOfDay`;
    
    chomp($end_sec);
    
    my $time = $end_sec - $start_sec / 1000.0;
    
    return $time;
    
}

parse_t_distribution;


while (my $cmd = <>) {

    my $sum_x = 0;
    my $sum_x_squared = 0;
    my $n = 0;
    
    my $variance = 100000;
    my $mean = 1;
    
    my $x = 0;
    my $s = 10;

    while($maxRuns > $n && ($s * $ttbl[$n-1][$myCI] > $plusMinus * $mean)) {
	
	$x = runit($cmd);
	
	$n++;
	$sum_x += $x;
	$sum_x_squared += ($x * $x);
	
	$variance = ($sum_x_squared/$n) - ($sum_x/$n)*($sum_x/$n);
	$mean = $sum_x / $n;
	
	my $s = sqrt($variance/$n);

    }

}


